<?php 
class NotiApplieModel extends Model {
	public function __construct(){
		$this->open(Model::MYSQL);
			$this->calledClass(get_class($this));
	}
	public function display(){
		$this->open(Model::MYSQL);
		return $this->getRecords($fields);
	}
	public function findArray($where=array()){
		$this->open(Model::MYSQL);
		return parent::findAsArray($where);
	}
	
	public function getAsObjectAdvanceSearch($fields=array(),$where=array()){
		$this->open(Model::MYSQL);
		return parent::getAsObject($fields,$where);
	}
	
	public function getObject($fields=array()){
		$this->open(Model::MYSQL);
		return parent::findAsObject($fields);
	}
	public function getList($page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria=""){
		$this->open(Model::MYSQL);
		return parent::getList($page,$perpage,$search,$searchFields,$sortFields,$sortCriteria);
	}
	public function delete($where=array(),$deleteSet=false){
		$this->open(Model::MYSQL);
		return parent::delete($where,$deleteSet);
	}
	public function numRows($result){
		$this->open(Model::MYSQL);
		return is_resource($result)?mysql_num_rows($result):null;
	}
	
	public function getApplyJobs($page=1,$perpage=20,$id){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$query="SELECT NotiApplies.id as sid,Jobs.*,Employers.company_name FROM NotiApplies
				INNER JOIN Jobs ON Jobs.id = NotiApplies.job_id
				INNER JOIN Employers ON Employers.id = Jobs.user_id 
				WHERE  NotiApplies.can_id=".$id." Order by NotiApplies.date_apply DESC ";
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	public function getCandidatid($id=""){
		$this->open(Model::MYSQL);
		$query="SELECT can_id From NotiApplies Where NotiApplies.can_id=".$id;
		$result = parent::query($query);
		$row=mysql_fetch_object($result);
		return is_object($row)?$row->can_id:"";
	}
	public function getCompApplyID($id="",$conid=""){
		$this->open(Model::MYSQL);
		if($id){
			 $query="Select NotiApplies.job_id From NotiApplies Where NotiApplies.job_id =".$id." AND can_id=".$conid;
		}
		$result = parent::query($query);
		$row=mysql_fetch_object($result);
		return is_object($row)?$row->job_id:"";
	}
	
	public function save($fields=array()){
		$this->open(Model::MYSQL);
		return parent::save($fields);
	}
	public function edit($fields=array(),$where=array()){
		$this->open(Model::MYSQL);
		return parent::update($fields,$where);
	}

	public function findObject($fields=array()){
		$this->open(Model::MYSQL);
		return $this->findAsObject($fields);
	}
	public function getJobApplied(){
		$query = "SELECT * FROM NotiApplies";
		$result = $this->query( $query );
		$data = array();
		while($row=mysql_fetch_object($result))$data[$row->can_id][$row->job_id]=$row;
		return $data;
	}
	public function getPage($can_id,$page=1,$perpage=20, $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT NotiApplies.id, NotiApplies.can_id,NotiApplies.status,Jobs.id as job_id,Jobs.title,Jobs.posting_date,Jobs.deadline,Jobs.email,Jobs.number_of_hire,Jobs.location_category,Jobs.qualification,Employers.company_name as by_company
				FROM Jobs INNER JOIN NotiApplies ON Jobs.id = NotiApplies.job_id INNER JOIN Employers ON Employers.id = Jobs.user_id WHERE NotiApplies.can_id = $can_id" ;
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	public function getApply($can_id,$page=1,$perpage=20,$search="", $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT NotiApplies.id, NotiApplies.can_id,NotiApplies.status, Jobs.*, Employers.company_name as by_company, Jobs.location_category,
		Jobs.qualification FROM Jobs
		INNER JOIN NotiApplies ON Jobs.id = NotiApplies.job_id
		INNER JOIN Employers ON Employers.id = Jobs.user_id WHERE NotiApplies.can_id = $can_id" ;
		if(!empty($searchFields)&&(isset($search)&&$search!=="")){
			$tempSearchFields=array();
			foreach($searchFields AS $field ){
				$tempSearchFields[]= " `$field` LIKE '%".$this->stringEscape($search)."%' " ;
			}
			$query .= " WHERE " . implode(" OR ",$tempSearchFields) . " AND can_id=$can_id " ;
		}
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	public function getPageEmployer($emp_id,$page=1,$perpage=20, $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT NotiApplies.id as apid,Candidates.username,Resumes.id as rid, NotiApplies.can_id,NotiApplies.status,Jobs.id as jid, Jobs.title, Employers.company_name as by_company, Jobs.location_category,
		Jobs.qualification FROM Jobs
		INNER JOIN NotiApplies ON Jobs.id = NotiApplies.job_id
		INNER JOIN Candidates ON Candidates.id = NotiApplies.can_id
		INNER JOIN Resumes ON Resumes.can_id = Candidates.id
		INNER JOIN Employers ON Employers.id = Jobs.user_id WHERE Employers.id = $emp_id" ;
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
}
